package com.zx.zxapibackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.zxapibackend.model.domain.User;
import com.zx.zxapibackend.model.vo.UserInterfaceLeftNumVo;
import com.zx.zxapibackend.service.UserService;
import com.zx.zxapicommon.common.BaseResponse;
import com.zx.zxapicommon.model.enums.ErrorCode;
import com.zx.zxapicommon.exception.BusinessException;
import com.zx.zxapibackend.mapper.UserInterfaceInfoMapper;
import com.zx.zxapibackend.service.UserInterfaceInfoService;
import com.zx.zxapicommon.model.entity.UserInterfaceInfo;
import com.zx.zxapicommon.model.to.LeftNumUpdateTo;
import com.zx.zxapicommon.utils.ResultUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Bro
 * @description 针对表【user_interface_info(用户调用接口关系)】的数据库操作Service实现
 * @createDate 2023-05-12 15:56:48
 */
@Service
public class UserInterfaceInfoServiceImpl extends ServiceImpl<UserInterfaceInfoMapper, UserInterfaceInfo>
        implements UserInterfaceInfoService {
    @Resource
    private UserService userService;
    @Resource
    private UserInterfaceInfoMapper userInterfaceInfoMapper;

    @Override
    public void validUserInterfaceInfo(UserInterfaceInfo userInterfaceInfo, boolean add) {

        if (userInterfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = userInterfaceInfo.getId();
        Long userId = userInterfaceInfo.getUserId();
        Long interfaceInfoId = userInterfaceInfo.getInterfaceInfoId();
        Integer totalNum = userInterfaceInfo.getTotalNum();
        Integer leftNum = userInterfaceInfo.getLeftNum();

        //参数校验
        if (userId <= 0 || userId <= 0 || interfaceInfoId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户id或者接口id有误");
        }

        if (leftNum < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口剩余调用次数应该大于0");
        }
    }

    @Override
    public boolean invokeCount(long interfaceInfoId, long userId) {
        if (interfaceInfoId <= 0 || userId <= 0) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "该用户接口关系不存在");
        }

        //判断用户是否有调用关系
        QueryWrapper<UserInterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("interfaceInfoId", interfaceInfoId);
        queryWrapper.eq("userId", userId);
        UserInterfaceInfo userInterfaceInfo = this.getOne(queryWrapper);
        if (userInterfaceInfo == null) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "您不用调用，请购买调用权限");
        }
        //判断是否还剩余调用次数
        Integer leftNum = userInterfaceInfo.getLeftNum();
        if (leftNum <= 0) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "您没有调用次数，请购买调用次数");
        }

        //执行调用
        UpdateWrapper<UserInterfaceInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("interfaceInfoId", interfaceInfoId);
        updateWrapper.eq("userId", userId);
        updateWrapper.setSql("totalNum = totalNum + 1, leftNum = leftNum - 1");

        boolean update = this.update(updateWrapper);
        return update;
    }

    /**
     * 获取当前登录用户的接口剩余调用次数
     *
     * @param request
     * @return
     */
    @Override
    public BaseResponse getUserInterfaceLeftNum(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long id = loginUser.getId();
        List<UserInterfaceLeftNumVo> userInterfaceLeftNumVo = userInterfaceInfoMapper.getUserInterfaceLeftNum(id);
        return ResultUtils.success(userInterfaceLeftNumVo);
    }

    /**
     * 更新用户可调用次数
     * @param leftNumUpdateTo
     * @return
     */
    @Override
    public BaseResponse updateUserLeftNum(LeftNumUpdateTo leftNumUpdateTo) {
        UpdateWrapper<UserInterfaceInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("userId",leftNumUpdateTo.getUserId());
        updateWrapper.eq("interfaceInfoId",leftNumUpdateTo.getInterfaceInfoId());
        updateWrapper.setSql("leftNum = leftNum + "+leftNumUpdateTo.getLockNum());
        boolean update = this.update(updateWrapper);
        return ResultUtils.success(update);
    }


}




